import request from '@/utils/request'

import { mapGetters } from 'vuex'
import CustomForm from '../Form/index.vue'
import { deepClone } from '@/utils'
export default {
  components: { CustomForm },
  props: ['type'],
  data() {
    return {
      loading: false,
      visible: false,
      list: [],
      query: {
        keywords: undefined
      },
      listQuery: {
        currentPage: 1,
        pageSize: 2000,
        sort: 'desc',
        sidx: ''
      },
      formVisible: false,
      brandOptions: [],
      sourseOptions: [],
      multipleSelection: [],
      selectTable: []
    }
  },
  computed: {
    ...mapGetters(['FQAList'])
  },
  watch: {
    multipleSelection: {
      handler: function (val) {

      },
      deep: true,
      immediate: true
    }
  },
  created() {
    this.$store.dispatch("base/getDictionaryData", { sort: "FqaSourse" }).then((res) => {
      this.sourseOptions = res;
    });
  },
  mounted() {
    this.selectTable = this.FQAList
  },
  methods: {
    handleSelectionChange (val){
      let that = this
      let oldArr = that.selectTable
      let arr = []
      oldArr.map(a_item => {
        let details = false
        that.list.map(b_item => {
          if (b_item.id === a_item.id) {
            details = true
          }
        })
        if (!details) {
          arr.push(a_item)
        }
      })
      that.selectTable = arr.concat(val)
    },
    dataFormRecords() {
      this.$store.dispatch('fqa/setListAll', this.selectTable)
      this.$emit('add')
      this.visible = false
    },
    getData() {
      let that = this
      that.list = []
      that.listLoading = true
      let _query = {
        ...that.listQuery,
        ...that.query
      }
      let query = {}
      for (let key in _query) {
        if (Array.isArray(_query[key])) {
          query[key] = _query[key].join()
        } else {
          query[key] = _query[key]
        }
      }
      request({
        url: `/api/extend/fqaentries`,
        method: 'GET',
        data: query
      }).then(res => {
        that.list = res.data.list
        that.total = res.data.pagination.total
        that.loading = false
        that.$nextTick(() => {
          that.selectTable.forEach(row => {
            that.list.map(item => {
              if (row.id === item.id) {
                that.$refs.multipleTable.$refs.CustomTable.toggleRowSelection(item);
              }
            })
          });
        })
      })
    },
    init() {
      this.visible = true
      this.loading = true

      this.$nextTick(() => {
        this.getData()
      }, 100)
    },
    search() {
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: 'desc',
        sidx: ''
      }
      this.init()
    },

    reset() {
      for (let key in this.query) {
        this.query[key] = undefined
      }
      this.listQuery = {
        currentPage: 1,
        pageSize: 2000,
        sort: 'desc',
        sidx: ''
      }
      this.getData()
    },

  }
}
